package com.qingmeng.applyjob.portal.service.impl;

import com.qingmeng.applyjob.mapper.*;
import com.qingmeng.applyjob.model.*;
import com.qingmeng.applyjob.portal.model.dto.ImproveResumeParam;
import com.qingmeng.applyjob.portal.model.dto.ImproveResumeParamOUT;
import com.qingmeng.applyjob.portal.model.vo.ResumeVo;
import com.qingmeng.applyjob.portal.model.vo.voclass.ProjectVo;
import com.qingmeng.applyjob.portal.model.vo.voclass.SkillVo;
import com.qingmeng.applyjob.portal.model.vo.voclass.WorkVo;
import com.qingmeng.applyjob.portal.service.ResumeService;
import com.qingmeng.applyjob.portal.utils.SecurityUtils;
import org.apache.ibatis.jdbc.Null;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class ResumeServiceImpl implements ResumeService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private WorkMapper workMapper;
    @Autowired
    private ProjectMapper projectMapper;
    @Autowired
    private SkillMapper skillMapper;
    @Autowired
    private EducationMapper educationMapper;

    @Transactional
    @Override
    public int improveResume(ImproveResumeParam improveResumeParam) {
        //更新用户信息
        User user = new User();
        user.setOpenid(improveResumeParam.getWechat());
        user.setGender(improveResumeParam.getGender());
        user.setAge(improveResumeParam.getAge());
        user.setPhone(improveResumeParam.getPhone());
        user.setEmail(improveResumeParam.getEmail());
        user.setAvatar(improveResumeParam.getAvatar());
        user.setIntroduce(improveResumeParam.getIntroduce());
        user.setUpdateTime(new Date());
        UserExample userExample = new UserExample();
        //根据当前用户id号进行信息更新
        userExample.createCriteria().andIdEqualTo(SecurityUtils.getCurrentUser().getId());
        if (userMapper.updateByExampleSelective(user,userExample) <= 0){
            return 0;
        }

        //添加工作经历
        Work work = new Work();
        work.setCompanyName(improveResumeParam.getCompanyName());
        work.setPosition(improveResumeParam.getWorkPosition());
        work.setIntroduce(improveResumeParam.getWorkIntroduce());
        work.setStartTime(improveResumeParam.getStartTime());
        work.setEndTime(improveResumeParam.getEndTime());
        work.setUserId(SecurityUtils.getCurrentUser().getId());
        work.setCreateTime(new Date());
        if (workMapper.insertSelective(work) <= 0){
            return 0;
        }

        //添加项目经验
        Project project = new Project();
        project.setProjectName(improveResumeParam.getProjectName());
        project.setPosition(improveResumeParam.getProjectPosition());
        project.setIntroduce(improveResumeParam.getProjectIntroduce());
        project.setStartTime(improveResumeParam.getProjectStartTime());
        project.setEndTime(improveResumeParam.getProjectEndTime());
        project.setUserId(SecurityUtils.getCurrentUser().getId());
        project.setCreateTime(new Date());
        if (projectMapper.insertSelective(project) <= 0){
            return 0;
        }

        //添加个人技能
        Skill skill = new Skill();
        skill.setUserId(SecurityUtils.getCurrentUser().getId());
        skill.setSkillName(improveResumeParam.getSkillName());
        skill.setLevel(improveResumeParam.getLevel());
        if (skillMapper.insertSelective(skill) <= 0){
            return 0;
        }

        //更新教育经历表
        Education education = new Education();
        education.setEducation(improveResumeParam.getEducation());
        education.setUserId(SecurityUtils.getCurrentUser().getId());
        education.setCollageName(improveResumeParam.getSchool());
        education.setMajor(improveResumeParam.getMajor());
        education.setStartTime(improveResumeParam.getEducationStartTime());
        education.setEndTime(improveResumeParam.getEducationEndTime());
        EducationExample educationExample = new EducationExample();
        educationExample.createCriteria().andUserIdEqualTo(SecurityUtils.getCurrentUser().getId());
        if (educationMapper.selectByExample(educationExample) != null && educationMapper.selectByExample(educationExample).size() > 0){
            EducationExample educationExample1 = new EducationExample();
            educationExample1.createCriteria().andUserIdEqualTo(SecurityUtils.getCurrentUser().getId());
            if (educationMapper.updateByExampleSelective(education,educationExample1) <= 0){
                return 0;
            }
        }else{
            if (educationMapper.insertSelective(education) <= 0){
                return 0;
            }
        }
        return 1;
    }

    @Transactional
    @Override
    public ResumeVo getMyResume() {
        ResumeVo resumeVo = new ResumeVo();
        Long userId = SecurityUtils.getCurrentUser().getId();
        UserExample userExample = new UserExample();
        userExample.createCriteria().andIdEqualTo(userId);
        List<User> users = userMapper.selectByExample(userExample);
        if (users != null && users.size() > 0){
            User user = users.get(0);
            resumeVo.setAvatar(user.getAvatar());
            resumeVo.setUsername(user.getUsername());
            resumeVo.setIntroduce(user.getIntroduce());
            resumeVo.setEmail(user.getEmail());
            resumeVo.setGender(user.getGender());
            resumeVo.setPhone(user.getPhone());
            resumeVo.setAge(user.getAge());
            resumeVo.setWechat(user.getOpenid());
            if (user.getUpdateTime() != null){
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String format = simpleDateFormat.format(user.getUpdateTime());
                resumeVo.setUpdateTime(format);
            }else {
                resumeVo.setUpdateTime(null);
            }

        }
        WorkExample workExample = new WorkExample();
        workExample.createCriteria().andUserIdEqualTo(userId);
        List<Work> workList = workMapper.selectByExample(workExample);
        List<WorkVo> workVos = new ArrayList<>();
        if (workList != null && workList.size() > 0) {
            for (Work work : workList) {
                WorkVo workVo = new WorkVo();
                workVo.setCompanyName(work.getCompanyName());
                workVo.setWorkPosition(work.getPosition());
                workVo.setWorkIntroduce(work.getIntroduce());
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM");
                if (work.getStartTime() != null) {
                    String format = simpleDateFormat.format(work.getStartTime());
                    workVo.setStartTime(format);
                }else {
                    workVo.setStartTime(null);
                }
                if (work.getEndTime() != null) {
                    String format1 = simpleDateFormat.format(work.getEndTime());
                    workVo.setEndTime(format1);
                } else {
                    workVo.setEndTime("至今");
                }
                workVos.add(workVo);
            }
            resumeVo.setWorkList(workVos);
        }
        ProjectExample projectExample = new ProjectExample();
        projectExample.createCriteria().andUserIdEqualTo(userId);
        List<Project> projectList = projectMapper.selectByExample(projectExample);
        List<ProjectVo> projectVos = new ArrayList<>();
        if (projectList != null && projectList.size() > 0){
            for (Project project : projectList){
                ProjectVo projectVo = new ProjectVo();
                projectVo.setProjectName(project.getProjectName());
                projectVo.setProjectPosition(project.getPosition());
                projectVo.setProjectIntroduce(project.getIntroduce());
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM");
                if (project.getStartTime() != null){
                    String format = simpleDateFormat.format(project.getStartTime());
                    projectVo.setProjectStartTime(format);
                }else {
                    projectVo.setProjectStartTime(null);
                }
                if (project.getEndTime() != null){
                    String format1 = simpleDateFormat.format(project.getEndTime());
                    projectVo.setProjectEndTime(format1);
                }else {
                    projectVo.setProjectEndTime("至今");
                }
                projectVos.add(projectVo);
            }
            resumeVo.setProjectList(projectVos);
        }
        SkillExample skillExample = new SkillExample();
        skillExample.createCriteria().andUserIdEqualTo(userId);
        List<Skill> skillList = skillMapper.selectByExample(skillExample);
        List<SkillVo> skillVos = new ArrayList<>();
        if (skillList != null && skillList.size() > 0){
            for (Skill skill : skillList){
                SkillVo skillVo = new SkillVo();
                skillVo.setSkillName(skill.getSkillName());
                skillVo.setLevel(skill.getLevel());
                skillVos.add(skillVo);
            }
            resumeVo.setSkillList(skillVos);
        }
        EducationExample educationExample = new EducationExample();
        educationExample.createCriteria().andUserIdEqualTo(userId);
        List<Education> educationList = educationMapper.selectByExample(educationExample);
        if (educationList != null && educationList.size() > 0){
            Education education = educationList.get(0);
            resumeVo.setEducation(education.getEducation());
            resumeVo.setSchool(education.getCollageName());
            resumeVo.setMajor(education.getMajor());
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");
            if (education.getStartTime() != null){
                String format = simpleDateFormat.format(education.getStartTime());
                resumeVo.setEducationStartTime(format);
            }else {
                resumeVo.setEducationStartTime(null);
            }

            if (education.getEndTime() != null){
                String format1 = simpleDateFormat.format(education.getEndTime());
                resumeVo.setEducationEndTime(format1);
            }else {
                resumeVo.setEducationEndTime("至今");
            }
        }
        return resumeVo;
    }
}
